package com.visionet.school.app.web.web;

import com.visionet.school.app.web.common.utils.PageInfo;
import com.visionet.school.app.web.common.web.WebContextFactory;
import com.visionet.school.app.web.entity.enums.EntrustEnums;
import com.visionet.school.app.web.entity.enums.FileEnums;
import com.visionet.school.app.web.entity.msg.vo.MsgVo;
import com.visionet.school.app.web.entity.vo.LoginAccountVo;
import com.visionet.school.app.web.entity.vo.StudentVo;
import com.visionet.school.app.web.service.MsgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hxpeng on 2017/3/7.
 */
@Controller
@RequestMapping("/msg")
public class MsgController {

    @Autowired
    private MsgService msgService;


    @RequestMapping(value = "/createNewMsg",method = RequestMethod.POST)
    public ResponseEntity<?> createNewMsg(@RequestBody MsgVo msgVo){
        if(msgVo == null || StringUtils.isBlank(msgVo.getMsgType()) || msgVo.getStudentIds() == null){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        if(msgVo.getMsgType().equals(FileEnums.AUDIO.toKey()) && StringUtils.isBlank(msgVo.getMediaId())){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        if(msgVo.getMsgType().equals(EntrustEnums.TEXT.toKey()) && StringUtils.isBlank(msgVo.getMsgContent())){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        Map<String,Object> map = new HashMap<>();
        if(msgVo == null){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        try {
            msgService.save(msgVo);
            map.put("result","success");
        }catch (Exception e){
            map.put("result","err");
            map.put("msg",e.getMessage());
        }
        return new ResponseEntity<Object>(map, HttpStatus.OK);
    }

    @RequestMapping(value = "/findMsgPageInfo",method = RequestMethod.POST)
    public ResponseEntity<?> findMsgPageInfo(@RequestBody MsgVo msgVo){
        if(msgVo == null){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        if(msgVo.getPageInfo() == null){
            msgVo.setPageInfo(new PageInfo());
        }
        LoginAccountVo loginAccountVo = WebContextFactory.getWebContext().getLoginAccountVo();
        List<StudentVo> studentVoList = loginAccountVo.getStudentVoList();
        if(studentVoList != null){
            List<String> studentIds = new ArrayList<>();
            for(StudentVo studentVo : studentVoList){
                studentIds.add(studentVo.getMemberId());
            }
            msgVo.setStudentIds(studentIds);
        }
        Map<String,Object> map = new HashMap<>();
        try {
            Map<String,Object> pageResult = msgService.getMsgPage(msgVo,"createDate");
            map.put("pageResult",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            map.put("stat","err");
        }
        return new ResponseEntity<Object>(map,HttpStatus.OK);
    }

}
